package com.seeyon.apps.cxgd.kit;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.common.formula.FormulaUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.script.ScriptException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 同程配置项
 */
public class TCConfig {
    private static Log log = LogFactory.getLog(TCConfig.class);

    /**
     * 获取token Api
     * @return
     */
    public static String getUrl(){
        return  getAddress()+"/openapi/api/OAuth/v2/GetAccessToken";
    }

    public static String getAddress(){
        String tcAddress = "";
        try {
            tcAddress = (String) FormulaUtil.getVar("same_business_trip_url");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return tcAddress;
    }

    public static String getAccount(){
        String tcAccount = "";
        try {
            tcAccount = (String) FormulaUtil.getVar("tcAccount");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return tcAccount;
    }

    public static String getPassword(){
        String tcPassword = "";
        try {
            tcPassword = (String) FormulaUtil.getVar("tcPassword");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return tcPassword;
    }

    public static String getSecret(){
        String tcSecret = "";
        try {
            tcSecret = (String) FormulaUtil.getVar("tcSecret");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return tcSecret;
    }


    /**
     * 外部差旅单同步接口
     */
    public static String getWBCLDTB(){
        return getAddress()+"/openapi/api/TravelApplyOrder/ApplyOrderSync?sign=";
    }


    /**
     * 人员同步接口
     * @return
     */
    public static String getPersonSyn(){
        return getAddress()+"/openapi/api/Employee/SyncEmployeeInfo?sign=";
    }

    /**
     * 同程单点登录接口
     * @return
     */
    public static String getSSOUrl(){
        return getAddress()+"/openapi/api/Employee/SSO?sign=";
    }


    /**
     * 获取对账单接口
     * @return
     */
    public static String getAccountUrl(){     
        return getAddress()+"/openapi/api/Order/QueryOnlineStatementPageList?sign=";
    }


    /**
     * 获取机票对账单接口
     * @return
     */
    public static String getJPAccountUrl(){
        return getAddress()+"/openapi/api/Order/QueryOnlineStatementFlightDetailPageList?sign=";
    }



    /**
     * 获取酒店对账单接口
     * @return
     */
    public static String getJDAccountUrl(){
        return getAddress()+"/openapi/api/Order/QueryOnlineStatementHotelDetailPageList?sign=";
    }


    /**
     * 获取火车对账单接口
     * @return
     */
    public static String getHCAccountUrl(){
        return getAddress()+"/openapi/api/Order/QueryOnlineStatementTrainDetailPageList?sign=";
    }


    /**
     * 获取火车订单详情信息
     * @return
     */
    public static String getHCOrderDetails(){
        return getAddress()+"/openapi/api/Train/GetTrainOrderDetail?sign=";
    }



    /**
     * 获取用车对账单接口
     * @return
     */
    public static String getYCAccountUrl(){
        return getAddress()+"/openapi/api/Order/QueryOnlineStatementCarDetailPageList?sign=";
    }




    /**
     * 获取同程token
     * @param account
     * @param password
     * @return
     */
    public static String getAccessToken(String account,String password){
        //获取UUId
        String uuid = UUID.randomUUID().toString();
        HashMap<String, Object> userInfo = new HashMap<>();
        userInfo.put("account",account);
        userInfo.put("password",password);
        Map<String, Object> parm = new HashMap<>();
        parm.put("isEncrypt",false);
        parm.put("random",uuid);
        parm.put("timestamp",new Date().getTime());
        parm.put("param", userInfo);
        String s1 = JSON.toJSONString(parm);
        return s1;
    }
}
